<template>
	<view class="content">
		<yHeader :title="tList[type]" :topBg="topBg" color="white" :showLeft="false"></yHeader>
		<view class="top">
			<image class="bg" src="/static/img/bg.png" mode="widthFix"></image>
			<view class="info" v-if="type != 'msg'">
				<view class="lnbox">
					<image src="/static/img/lne.png" mode="widthFix"></image>
					<view class="name" v-if="is_login">
						<view class="hi">HI ~</view>
						<view class="name-text">{{info.username}}</view>
					</view>
					<view class="name" v-else @click="doLogin">
						<text class="hi">HI ~</text>
						<text>请登录</text>
					</view>
				</view>
				<view class="headurl" v-if="is_login">
					<image :src="info.head_url" mode="aspectFill"></image>
				</view>
			</view>
		</view>
		<view class="main">
			<aHome v-if="type == 'home'" @outLogin="outLogin"></aHome>
		</view>

	</view>
</template>

<script>
	import {
		getToken,
		removeToken
	} from "@/utils/auth.js"
	import {
		GetOpenid,
		UserIndex
	} from "@/api/api.js"
	export default {
		data() {
			return {
				type: 'home',
				topBg: '',
				tList: {
					home: '个人中心'
				},
				style: '',
				is_login: false,
				info: [],
				userinfo: []
			}
		},
		onLoad(e) {
			if (e.code) {
				uni.request({
					url: this.$config.requestUrl + '/Open/get_user_all',
					data: {
						code: e.code
					},
					success: (res) => {
						if (res.data.status == 1) {
							uni.setStorageSync('token', res.data.data.token)
							this.UserIndex()
						}
					}
				})
			} else {
				// #ifdef MP-WEIXIN
				this.doLogin()
				// #endif
			}
			uni.$on('changeType', (e) => {
				this.type = e
			})
			if (getToken() != '') {
				this.is_login = true
			} else {
				this.is_login = false
			}
		},
		onShow() {
			this.UserIndex()
		},
		onPageScroll(e) {
			if (e.scrollTop > 5) {
				this.topBg = '#4F9DFD';
			} else {
				this.topBg = '';
			}
		},
		onUnload() {
			uni.$off()
		},
		methods: {
			outLogin() {
				this.is_login = false
				this.info = []
				this.userinfo = []
			},
			getPhone(e) {
				console.log(e);
			},
			UserIndex() {
				UserIndex().then(res => {
					if (res.status == 1) {
						this.info = res.data.info
						this.userinfo = res.data
						this.is_login = true
					} else {
						this.is_login = false
					}
				})
			},
			quickLogin() {
				GetOpenid({
					openid: this.openid
				}).then(res => {
					uni.showToast({
						title: res.message,
						icon: "none"
					})
					if (res.status == 1) {
						uni.setStorageSync('token', res.data.token)
						this.userIndex()
					}
				})
			},
			doLogin() {
				const that = this;
				window.location = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + this.$config.WXAPPID +
					'&redirect_uri=' +
					this.$config.REDIRECT_login_URL +
					'&response_type=code&scope=snsapi_userinfo&state=abcdefghijklmnopqrstuvwxyz#wechat_redirect';
			},
			userIndex() {

			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: 54px;
		background: #F9FAFD;
	}


	.top {
		.bg {
			width: 100%;
			margin-top: -44px;
		}
	}

	.info {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 4%;
		margin-top: -90px;
		position: relative;
		z-index: 1000;

		.lnbox {
			width: 60%;
			position: relative;
			z-index: 99;

			image {
				width: 100%;
			}

			.name {
				position: absolute;
				display: flex;
				align-items: center;
				padding-left: 22px;
				box-sizing: border-box;
				top: 50%;
				transform: translate(0, -50%);
				margin-top: -3px;
				color: #fff;
				font-weight: bold;
				width: 100%;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;

				.hi {
					font-weight: normal;
					color: #4F9DFD;
					margin-right: 12px;
					width: 25%;
					display: block;
				}
			}
		}

		.headurl {
			width: 60px;
			height: 60px;

			image {
				width: 100%;
				height: 100%;
				border-radius: 50%;
			}
		}

		.play {
			color: #FE8E5E;
			padding: 2px 8px;
			border-radius: 4px;
			font-size: 14px;
			background: rgba(255, 255, 255, 0.78);
		}
	}

	.main {
		margin-top: 60px;
	}
</style>